<?php



namespace app\admin\service;


use think\facade\Db;

/**
 * 权限验证服务
 * Class AuthService
 * @package app\common\service
 */
class AuthService
{

    /**
     * 用户ID
     * @var null
     */
    protected $adminId = null;

    /**
     * 默认配置
     * @var array
     */
    protected $config = [
        'auth_on'          => true,              // 权限开关
        'system_admin'     => 'system_admin',    // 用户表
        'system_auth'      => 'system_auth',     // 权限表
        'system_auth_node' => 'system_auth_node',// 权限-节点表
    ];

    /***
     * 构造方法
     * AuthService constructor.
     * @param null $adminId
     */
    public function __construct($adminId = null)
    {
        $this->adminId = $adminId;
        $this->menu = app('app\common\model\system\SystemMenu');
        $this->admin = app('app\common\model\system\SystemAdmin');
        $this->auth = app('app\common\model\system\SystemAuth');
        $this->node = app('app\common\model\system\SystemAuthNode');
        return $this;
    }

    /**
     * 检测检测权限
     * @param null $node
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkNode($node = null)
    {
        // 判断是否为超级管理员
        if ($this->adminId == 1) {
                return true;
        }

        // 判断权限验证开关
        if ($this->config['auth_on'] == false) {
            return true;
        }
        // 判断是否加入节点控制，优先获取缓存信息
        $nodeInfo = $this->menu
            ->where(['href' => $node])
            ->find();

        if (empty($nodeInfo)) {
            return false;
        }

        /*if (@$nodeInfo['is_auth'] == 0) {
            return true;
        }*/
        // 用户验证，优先获取缓存信息
        $adminInfo = $this->admin
            ->where('id', $this->adminId)
            ->find();

        if($adminInfo->auth_ids == 0){
            return true;
        }
        if (empty($adminInfo) || $adminInfo['status'] != 1 || empty($adminInfo['auth_ids'])) {
            return false;
        }
        // 判断该节点是否允许访问
        $allNode = $this->getAdminNode();
        if (in_array($node, $allNode)) {
            return true;
        }
        return false;
    }


    /**
     * 获取当前管理员所有节点
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminNode()
    {
        $nodeList = [];
        $adminInfo = $this->admin
            ->where([
                'id'     => $this->adminId,
                'status' => 1,
            ])->find();

        if (!empty($adminInfo)) {
            $buildAuthSql = $this->auth
                ->distinct(true)
                ->whereIn('id', $adminInfo['auth_ids'])
                ->field('id')
                ->buildSql(true);
            $buildAuthNodeSql = $this->node
                ->distinct(true)
                ->where("auth_id IN {$buildAuthSql}")
                ->field('node_id')
                ->buildSql(true);
            $nodeList = $this->menu
                ->distinct(true)
                ->where("id IN {$buildAuthNodeSql}")
                ->column('href');
        }

        return $nodeList;
    }



}